<?php
// 面包屑
$this->breadcrumbs=array(
    'Products'=>array('index'),
    'Manage',
);
?>

<?php
/**
 * 生成产品的小图列表
 * @param Array 产品图片信息数组
 * @return String 图片列表HTML
 */
function genPicturesList($data){
    if(!empty($data->pictures)) {
        $ret = '<ul class="thumb-list" id="thumbList'.'">';
        foreach ($data->pictures as $picture){
            if(isset($data->cover) && $picture->id == $data->cover)
                $ret .= '<li class="cover">';
            else
                $ret .= '<li>';
            $ret .='<span>Cover</span><div class="pic">';
            $ret .= $data->getPicture($picture->id, 'small');
            $ret .= '</div>';
            $ret .='</li>';
        }
        $ret .='</ul>';
    } else {
        $ret ='<span class="label label-warning">No pictures</span>';
    } 

    return $ret;
}
?>

<div class="page-header">
    <?php echo CHtml::link('<i class="icon-plus icon-white"></i> Create product', array('product/create'), array(
        'class'=>'btn btn-primary',
    )) ?>
    <h1>Manage Products</h1>
</div>

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'product-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
        array(
            'name'=>'thumbs',
            'type'=>'raw',
            'value'=>'genPicturesList($data)',
            'header'=>'',
            'filter'=>false,
            'sortable'=>false,
        ),
        array(
            'name'=>'category_id',
            'type'=>'raw',
            'value'=>'$data->category->name',
            'filter'=>CHtml::listData(Category::model()->findAll(), 'id', 'name'),
        ),
        array(
            'name'=>'os_id',
            'type'=>'raw',
            'value'=>'$data->os->name',
            'filter'=>CHtml::listData(Os::model()->findAll(), 'id', 'name'),
        ),
        'name',
		'model',
		/*'id',
		'category_id',
		'os_id',
		'name',
		'model',
		'chip',
		'size',
		'weight',
		'display',
		'storage',
		'memory',
		'sensors',
		'product',
		'camera',
		'battery',
		'power',
		'audio',
		'video',
		'extra',
		'picture',
		'details',
		*/
		array(
			'class'=>'CButtonColumn',
			'template'=>'{view} {update} {delete}',
            'viewButtonUrl'=>'Yii::app()->baseUrl."/product/".$data->id',
            'viewButtonImageUrl'=>Yii::app()->baseUrl.'/css/admin/gridview/view.png',
            // Tooltip for view button
            'viewButtonOptions'=>array(
                'target'=>'_blank',
                'rel'=>'tooltip',
                'data-original-title'=>'Details',
                'title'=>'',
            ),
            'updateButtonImageUrl'=>Yii::app()->baseUrl.'/css/admin/gridview/update.png',
            // Tooltip for update button
            'updateButtonOptions'=>array(
                'rel'=>'tooltip',
                'data-original-title'=>'Edit',
                'title'=>'',
            ),
            'deleteButtonImageUrl'=>Yii::app()->baseUrl.'/css/admin/gridview/delete.png',
            // Tooltip for delete button
            'deleteButtonOptions'=>array(
                'rel'=>'tooltip',
                'data-original-title'=>'Delete',
                'title'=>'',
            ),
            'buttons'=>array(
                'delete'=>array(
                    'click'=>"function( e ){
                        e.preventDefault();
                        $('#modalUpdate .modal-body').children(':eq(0)').empty(); // Stop auto POST
                        $('#modalUpdate .modal-header h3').html('Delete Confirm');
                        updateModal($(this).attr('href'));
                        $('#modalUpdate').modal('show');
                    }",
                ),
            ),
		),
	),
)); ?>

<!-- Modal for Update -->
<?php $this->beginWidget('zii.widgets.tui.CTuiModal', array(
    'id'=>'modalUpdate',
    'options'=>array(
        'title'=>'Update Product',
    ),
)); ?>
<div class="modal-body form-wrap"></div>
<?php $this->endWidget(); ?>

<?php
$updateJS = CHtml::ajax(array(
    'url'=>"js:url",
    'data'=>"js:form.serialize() + action",
    'type'=>'post',
    'dataType'=>'json',
    'success'=>"function(data)
    {
        if(data.status == 'failure')
        {
            $('#modalUpdate div.modal-body').html(data.content);
            $('#modalUpdate div.modal-body form input[type=submit]')
                .die() // Stop from re-binding event handlers
                .live('click', function(e){ // Send clicked button value
                e.preventDefault();
                updateModal(false, $(this).attr('name'));
            });
        }
        else
        {
            $('#modalUpdate div.modal-body').html(data.content);
            if(data.status == 'success') // Update all grid views on success
            {
                $('div.grid-view').each(function(){ // Change the selector if you use different class or element
                    $.fn.yiiGridView.update($(this).attr('id'));
                });
            }
            $('#modalUpdate').modal('hide');
            $('#modalUpdate .modal-body').children(':eq(0)').empty();
        }
    }"
)); ?>

<?php
Yii::app()->clientScript->registerScript('updateModal', "function updateModal(url, act)
    {
        var action = '';
        var form = $('#modalUpdate div.modal-body form');
        if(url == false)
        {
            action = '&action=' + act;
            url = form.attr('action');
        }
        {$updateJS}
    }" 
); ?>

<?php
$setCoverUrl = $this->createUrl('product/setCover');
Yii::app()->clientScript->registerScript('setCover', "$('.thumb-list li').live('click', function(){
        var picId = $(this).find('img').attr('rel');
        $.ajax({
            url: '".$setCoverUrl."',
            data: {pictureId:picId},
            type: 'post',
            dataType: 'json',
            success: function(data){
                //console.log(data);
                if (data.status == 'failure')
                {

                }
                else
                {
                    if(data.status == 'success') // Update all grid views on success
                    {
                        $('div.grid-view').each(function(){ // Change the selector if you use different class or element
                            $.fn.yiiGridView.update($(this).attr('id'));
                        });

                        $('#modalUpdate div.modal-body').html(data.content);
                        $('#modalUpdate').modal('show')
                            .on('hidden', function(){
                                $('#modalUpdate .modal-body').empty();
                            });
                        setTimeout(\"$('#modalUpdate').modal('hide') \",2000);
                    }
                }
            }
        });
    });"
);
?>